package com.topvision.jack.camerasdk2.camera.callback;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.topvision.jack.camerasdk2.R;
import com.topvision.jack.camerasdk2.camera.PhotoLoaderTask;
import com.topvision.jack.camerasdk2.camera.commom.CameraConstants;
import com.topvision.jack.camerasdk2.camera.view.TvPhotoView;

import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by gushenjie on 17/12/26.
 */

public class PhotoSimpleOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {

    List<View> views;
    List<String> photos;
    Context context;
    OnShowPhotoListener onShowPhotoListener;
    //滑动前索引位置
    int mViewPagerIndex;
    //滑动后位置
    int position;
    TvPhotoView tvPhotoView;

    public PhotoSimpleOnPageChangeListener(Context context,TvPhotoView tvPhotoView){
        this.tvPhotoView = tvPhotoView;
        this.views = tvPhotoView.getViews();
        this.photos = tvPhotoView.getPhotos();
        this.context = context;
        this.onShowPhotoListener = tvPhotoView.getOnShowPhotoListener();

    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // This space for rent
    }

    @Override
    public void onPageSelected(int position) {
        Log.e(CameraConstants.TAG,"position = "+position+",mViewPagerIndex="+mViewPagerIndex);
        this.position = position;
        callbackUser();
        PhotoView currentPhoto = views.get(position).findViewById(R.id.photo_view);
        new PhotoLoaderTask(context, currentPhoto).execute(photos.get(position));
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if(state==1){//state有三种状态下文会将，当手指刚触碰屏幕时state的值为1，我们就在这个时候给mViewPagerIndex 赋值。
            mViewPagerIndex = tvPhotoView.getCurrentItem();
           // Log.e(CameraConstants.TAG,"mViewPagerIndex="+mViewPagerIndex);
        }
    }

    //回调给上层用户
    public  void callbackUser(){
        if(onShowPhotoListener!=null){
            if(mViewPagerIndex<position){
                onShowPhotoListener.doLeft();

            }else{
                onShowPhotoListener.doRight();

            }
        }
    }

}
